import {http} from "@/utils/http";
// 这里编写 后端接口 HTTP请求代码

export type BaseResponse<T> = {
  errorMsg: string;
  errorCode: string;
  success: boolean;
  content: T;
}
// let HanjunTest extend BaseResponse<HanjunTest> {}
export type ProcessItemDTO = BaseResponse<{
    data: [{
      /** 头像 */
      name: string;
      /** 用户名 */
      no: string;
      /** 昵称 */
      formInstanceId: string;
      /** 当前登录用户的角色 */
      // roles: Array<string>;
      /** 按钮级别权限 */
      id: BigInteger;
      /** `token` */
      corpId: string;
      /** 用于调用刷新`accessToken`的接口时所需的`token` */
      creator: string;
      creatorName: string;
    }]
}>;

export type ProcessIdData = {
  id: string;  // 假设id是字符串类型
  flowCode: string;
}

export type ResponseSimpleDTO = {
  success: boolean;  // 假设id是字符串类型
  errorMessage: string;
  errorCode: string;
  errorMsg: string;
}

export type RefreshTokenResult = {
  success: boolean;
  data: {
    /** `token` */
    accessToken: string;
    /** 用于调用刷新`accessToken`的接口时所需的`token` */
    refreshToken: string;
    /** `accessToken`的过期时间（格式'xxxx/xx/xx xx:xx:xx'） */
    expires: Date;
  };
};

/** 登录4 test */
export const getLogin2 = (data?: object) => {
  // debugger
  console.log("getLogin2");
  // 要求支持跨域
  // return http.request<UserResult>("post", "/login", { data, withCredentials: true });
  // 未要求支持跨域
  return http.request<ProcessItemDTO>("post", "http://localhost:8080/palm/mock/demo.json", {
    data,
    withCredentials: true
  });
};

/**
 * 流程定义列表
 * @param data
 */
export const queryProcsDefList = (data?: object) => {
  // debugger
  console.log("queryProcsDefList");
  // 要求支持跨域
  // return http.request<UserResult>("post", "/login", { data, withCredentials: true });
  return http.request<ProcessItemDTO>("post", "http://127.0.0.1:8080/flow/definition/list.json", {
    data,
    withCredentials: true
  });
};

/**
 * 发起流程实例
 * @param data
 */
export const startProcsInstance = (data?: ProcessIdData) => {
  // debugger
  console.log("发起流程实例");
  // 要求支持跨域
  // return http.request<UserResult>("post", "/login", { data, withCredentials: true });
  return http.request<ProcessItemDTO>("post", "http://127.0.0.1:8080/palm/procs/startProcessInstance.json" /*+ data.id*/, {
    withCredentials: true
  });
};


/**
 * 发布流程
 * @param data
 */
export const publishProcsDef = (data?: ProcessIdData) => {
  // debugger
  console.log("publishProcsDef");
  // 要求支持跨域
  // return http.request<UserResult>("post", "/login", { data, withCredentials: true });
  return http.request<ProcessItemDTO>("get", "http://127.0.0.1:8080/flow/definition/publish/1285376095981539328" /*+ data.id*/, {
    withCredentials: true
  });
};

/**
 * http://localhost:8080/flow/definition/xmlString/1285376095981539328
 * @param data
 */
export const getProcsDefSchema = (data?: ProcessIdData) => {
  // debugger
  console.log("getProcsDefSchema");
  // 要求支持跨域
  // return http.request<UserResult>("post", "/login", { data, withCredentials: true });
  return http.request<HanjunTest>("get", "http://127.0.0.1:8080/flow/definition/xmlString/" + data.id, {
    withCredentials: true
  });
};

/** 刷新`token` */
export const refreshTokenApi = (data?: object) => {
  return http.request<RefreshTokenResult>("post", "/refresh-token", {data});
};
